package com.dog.basics.api.controller.common;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dog.basics.bean.dto.api.common.PasswordDto;
import com.dog.basics.bean.dto.api.common.PayPasswordDto;
import com.dog.basics.bean.dto.api.common.UpdateEmailDto;
import com.dog.basics.bean.dto.api.common.UpdateMobileDto;
import com.dog.basics.bean.model.common.DogUser;
import com.dog.basics.bean.model.common.DogUserConnect;
import com.dog.basics.bean.vo.api.common.ApiUserEditDto;
import com.dog.basics.bean.vo.api.common.UserInfoVo;
import com.dog.basics.common.constant.SwaggerConstants;
import com.dog.basics.common.util.R;
import com.dog.basics.security.util.SecurityUtils;
import com.dog.basics.service.common.IDogUserConnectService;
import com.dog.basics.service.common.IDogUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/user")
@Api(value = "用户接口", tags = SwaggerConstants.API_ALL)
@AllArgsConstructor
public class ApiUserController {
    private final IDogUserService userService;
    private final IDogUserConnectService dogUserConnectService;

    /**
     * @Description: 查看用户信息
     * @Param:
     * @return: UserInfoVo
     * @Author: wph
     * @Date: 2020/3/27 0027
     */
    @GetMapping("/userInfo")
    @ApiOperation(value = "查看用户信息", notes = "根据用户ID（userId）获取用户信息", tags = SwaggerConstants.API_MY)
    public R<UserInfoVo> userInfo() {
        Long userId = SecurityUtils.getUser().getUserId();
        DogUser user = userService.getById(userId);
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtil.copyProperties(user, userInfoVo);
        List<DogUserConnect> userConnectList = dogUserConnectService.list(Wrappers.<DogUserConnect>lambdaQuery().eq(DogUserConnect::getUserId, SecurityUtils.getUser().getUserId()));

        if (CollUtil.isNotEmpty(userConnectList)) {
            DogUserConnect dogUserConnect = userConnectList.get(0);
            if (StrUtil.isNotBlank(dogUserConnect.getMiniOpenid())) {
                userInfoVo.setSfBd(1);
            } else {
                userInfoVo.setSfBd(0);
            }
        } else {
            userInfoVo.setSfBd(0);
        }
        return R.ok(userInfoVo);
    }

    /**
     * @Description: 修改用户信息
     * @Param:
     * @return: UserInfoVo
     * @Author: wph
     * @Date: 2020/3/27 0027
     */
    @PutMapping("/editInfo")
    @ApiOperation(value = "修改用户信息(包含头像)", notes = "修改用户信息(包含头像)", tags = SwaggerConstants.API_MY)
    public R<Boolean> editInfo(@Valid @RequestBody ApiUserEditDto obj) {
        return R.ok(userService.editInfo(obj));
    }


    /**
     * 手机号修改发送验证码
     *
     * @param phone
     * @return guonh 2019年12月21日
     */
    @ApiOperation(value = "手机号修改发送验证码", notes = "手机号修改发送验证码", tags = SwaggerConstants.API_MY)
    @ApiImplicitParam(name = "phone", value = "手机号码", required = true, dataType = "String")
    @GetMapping("/api/editPhoneSms")
    public R<Boolean> editPhoneSms(@RequestParam String phone) {
        return R.ok(userService.editPhoneSms(phone));
    }

    /**
     * 邮箱修改发送验证码
     *
     * @param email
     * @return guonh 2019年12月21日
     */
    @ApiOperation(value = "邮箱修改发送验证码", notes = "邮箱修改发送验证码", tags = SwaggerConstants.API_MY)
    @ApiImplicitParam(name = "email", value = "邮箱", required = true, dataType = "String")
    @GetMapping("/api/editEmailSms")
    @ApiIgnore
    public R<Boolean> editEmailSms(@RequestParam String email) {
        return R.ok(userService.editEmailSms(email));
    }

    /**
     * @Description: 修改手机号
     * @Param: oldPhone, phone, code
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/3/27 0027
     */
    @ApiOperation(value = "修改手机号", notes = "修改手机号", tags = SwaggerConstants.API_MY)
    @PutMapping("/updateMobile")
    public R<Boolean> updateMobile(@Valid @RequestBody UpdateMobileDto dto) {
        return R.ok(userService.updateMobile(dto));
    }

    /**
     * @Description: 修改邮箱
     * @Param: email, code
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/3/27 0027
     */
    @ApiOperation(value = "修改邮箱", notes = "修改邮箱", tags = SwaggerConstants.API_MY)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "email", value = "邮箱", required = true, dataType = "String"),
            @ApiImplicitParam(name = "code", value = "短信验证码", required = true, dataType = "String"),
    })
    @PutMapping("/updateEmail")
    @ApiIgnore
    public R<Boolean> updateEmail(@Valid @RequestBody UpdateEmailDto dto) {
        return R.ok(userService.updateEmail(dto));
    }

    /**
     * @Description: 修改密码
     * @Param: oldpassword, password
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/3/27 0027
     */
    @ApiOperation(value = "修改密码", notes = "修改密码", tags = SwaggerConstants.API_MY)
    @PutMapping("/editPassword")
    @ApiIgnore
    public R<Boolean> editPassword(@Valid @RequestBody PasswordDto passwordDto) {
        return R.ok(userService.editPassword(passwordDto));
    }

    /**
     * @Description: 修改支付密码
     * @Param: oldpassword, password
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/3/27 0027
     */
    @ApiOperation(value = "修改支付密码", notes = "修改支付密码", tags = SwaggerConstants.API_MY)
    @PutMapping("/editPayPassword")
    public R<Boolean> editPayPassword(@Valid @RequestBody PayPasswordDto obj) {
        return R.ok(userService.editPayPassword(obj));
    }
}
